package csu.web.jpetstore.web.servlet;

import com.alibaba.fastjson.JSON;
import csu.web.jpetstore.domain.Account;
import csu.web.jpetstore.service.AccountService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class IdValidServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id=req.getParameter("id");
        AccountService service=new AccountService();
        Account account= service.getAccount(id);

        String result=new String();
        if(account!=null){
            result="This id is engaged.";
            System.out.println(result);
        }

        String re= JSON.toJSONString(result);
        resp.setContentType("text/json");
        PrintWriter out=resp.getWriter();
        out.println(re);
    }
}
